<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav mui-bar-transparent">	
			<a class="mui-icon mui-icon-back mui-action-back mui-pull-left"></a>
			<a class="mui-icon mui-icon-list mui-pull-right" id="btnSearch"></a>
		</header>
		<div class="mui-content">
			<div class="mui-content-padded">
				<div class="mui-row" style="text-align: center;">
					<h4 v-html="Title">食药全通</h4>
				</div>
				<div class="mui-button-row">
					<button type="button" class="mui-btn mui-btn-primary">追更新</button>
					<button type="button" class="mui-btn mui-btn-primary" v-on:click="openWindow()">开始阅读</button>
				</div>
				<div class="mui-row" style="margin-top: 10px;">
					<div  v-for="item in Label" style="float: left;margin: 5px;">
						<button type="button" class="mui-btn mui-btn-danger mui-btn-outlined" v-html="item">
						</button>
					</div>
				</div>
				<div class="mui-row" style="padding: 10px;">
					<p style="color: #333;">
						&nbsp;&nbsp;&nbsp;&nbsp;
						<span  v-html="Summary"></span>
					</p>
				</div>
			</div>
			
			
		</div>
		<script src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/vue.min.js" ></script>
		<script type="text/javascript" src="../js/app.js" ></script>
		<script type="text/javascript">
			mui.init();
			
			function getDefaultData() {
				return {
					"Id":"20f4544b-1583-4870-b9ad-eb1340f85c2b",
					"Code":"",
					"Title":"药品管理法实施条例",
					"RefClassifyId":"",
					"ClassifyName":"",
					"Summary":"",
					"Label":"",
					"Content":"",
					"DispatchCorp":"",
					"DispatchDate":null,
					"EffectiveDate":null,
					"AttachmentUrl":"",
					"RefParentId":"",
					"ParentFile":"",
					"DataState":null,
					"InvalidDate":null,
					"RefReplaceId":"",
					"ReplaceFile":"",
					chapterKeys:[],
				}
			}

			var vm = new Vue({
				el: '.mui-content',
				data: getDefaultData(),
				methods: {
					resetData: function() {//重置数据
						Object.assign(this.$data, getDefaultData());
					}
				}
			});

			//监听自定义事件，获取新闻详情
			document.addEventListener('get_detail', function(event) {
				var item = event.detail.Current;
				console.log(JSON.stringify(item));
				if(!item) {
					return;
				}
				vm.Id = item.Id;
				vm.Title = item.Title;
				vm.Summary = item.Summary;
				
//				/*加载数据*/
				var setting = app.getSetting();
				var queryJson ={
					RefContentId:vm.Id
				};
				/*读取文档所有章节*/
				var ajaxUrl = setting.ServerUrl +"/api/TrainChapter/GetKeyList?queryJson="+JSON.stringify(queryJson);
				console.log(ajaxUrl);
				plus.nativeUI.showWaiting("正在读取数据，请稍候...");
				mui.ajax(ajaxUrl,{
					dataType:'json',//服务器返回json格式数据
					type:'get',//HTTP请求类型
					timeout:10000,//超时时间设置为10秒；
					headers:{'Content-Type':'application/json'},	              
					success:function(result){
						plus.nativeUI.closeWaiting();
						//服务器返回响应，根据响应结果，分析是否登录成功；
						console.log(JSON.stringify(result));
						if(result.ResultType != 0){
							mui.toast(result.Content);
							return;
						}
						vm.chapterKeys = JSON.parse(result.AppendData);
					},
					error:function(xhr,type,errorThrown){
						plus.nativeUI.closeWaiting();
						//异常处理；
						console.log(type);
						mui.toast("加载数据异常！");
					}
				});
			});

			//重写返回逻辑
			mui.back = function() {
				var self = plus.webview.currentWebview();
				self.hide("auto", 300);
			}
			
			//窗口隐藏时，重置页面数据
			mui.plusReady(function () {
				var self = plus.webview.currentWebview();
				self.addEventListener("hide",function (e) {
					window.scrollTo(0, 0);
					vm.resetData();
				},false);
			})
			
			function openWindow(){
				/*保存更新用户阅读文档信息*/
				var doc = app.getDocument(vm.Id);
				if(doc == undefined){
					doc = {Id:vm.Id,Title:vm.Title,ChapterKeys:vm.chapterKeys,ReadIndex:0};
				}
				else{
					doc.Title = vm.Title;
					doc.ChapterKeys = vm.chapterKeys;
				}
				if(doc.ReadIndex == undefined)
					doc.ReadIndex = 0;	
				app.addDocument(vm.Id,doc);
				console.log("doc="+JSON.stringify(doc));
				
				mui.openWindow({
					id:"chapterInfo",
					url:"chapterInfo.html",
					show:{aniShow: 'pop-in'},
					extras: {refContentId: vm.Id,readIndex:doc.ReadIndex},
					waiting: {autoShow: true, /*自动显示等待框，默认为true*/}
				});
			}
		</script>
	</body>

</html>